package main

import (
	"/logservice"
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"time"
)

func readFromFile() {
	f, err := os.Open("./main.go")
	if err != nil {
		fmt.Println("open error:", err)
		return
	}
	defer f.Close()
	tmp := make([]byte, 128)
	for {
		n, err := f.Read(tmp)
		if n == 0 {
			//fmt.Println("文件读完")
			return
		} else if err != nil && err != io.EOF {
			fmt.Println("其它错误", err)
			return
		}

		fmt.Printf("读了%d个字节\n", n)
		fmt.Println(string(tmp[:n]))
	}
}

//利用bufio读取文件
func readEfromFilebyBufio() {
	f, err := os.Open("./main.go")
	if err != nil {
		fmt.Println("file open err:", err)
		return
	}
	defer f.Close()
	reader := bufio.NewReader(f) //创建一个用来从文件中读内容的对象
	for {
		line, err := reader.ReadString('\n')
		if err == io.EOF {
			return
		}
		if err != nil {
			fmt.Println("read line failed,err:", err)
			return
		}
		fmt.Print(line)
	}
}

//利用utileio读取整个文件
func readFromFilebyIoutile(fn string) {

	buf, err := ioutil.ReadFile(fn)
	if err != nil {
		fmt.Println("file read error", err)
		return
	}
	fmt.Print(string(buf))
}

func useBufio() {
	var s string
	reader := bufio.NewReader(os.Stdin)
	fmt.Println("请输入内容:")
	s, _ = reader.ReadString('\n')
	fmt.Printf("你输入的内容是:%s\n", s)
}

func main() {
	//readEfromFilebyBufio()
	//readFromFilebyIoutile("./main.go")
	// logger.Trace()
	// logger.Debug()
	// logger.Warning()
	// logger.Info()
	// logger.Error()
	//useBufio()
	//logOut("./log.txt")
	log := logservice.NewFileLogger("INFO", "./", "log1", 16*1024)
	for {
		log.Error("错误测试")
		log.Info("信息测试")
		log.Debug("调试测试")
		id := 100
		name := "stormfat"
		log.Trace("调试")
		log.Fatal("调试,id:%d,name:%s", id, name)
		//time.Sleep(time.Second * 2)
	}
}

func logOut(fn string) {
	f, err := os.OpenFile(fn, os.O_CREATE|os.O_RDWR, 0644)
	if err != nil {
		fmt.Println("file open error:", err)
		return
	}
	log.SetOutput(f)
	for {
		log.Print("这是一条测试日志")
		time.Sleep(time.Second * 2)
	}
}
